<?php

class m_project extends CI_Model{
	
	function _list($data){
		extract($data);
		$sql = "SELECT 
					project_name,
					project_code,
					project_value,
					project_date,
					project_id,
                    m_cabang_id
				FROM t_project
                WHERE 1=1 
		";
         
		if (@$_SESSION[SESSION_GROUPDIVID] == "1") $sql .= " AND m_cabang_id = '".@$_SESSION[SESSION_CABANGID]."'";
		if (@$project_name != "") $sql .= " AND project_name like '%".@$project_name."%'";
		if (@$project_code != "") $sql .= " AND project_code like '%".@$project_code."%'";
		if (@$project_value != "") $sql .= " AND project_value like '%".@$project_value."%'";
		
		$query = $this->db->query($sql);
		$records = $query->num_rows();
		
		$sql = "SELECT * FROM (".$sql.") k LIMIT ".(($page - 1) * $rows).",".$rows;
		$query = $this->db->query($sql);
		$result = $query->result();
		
		return '{ 
		  "total": "'.ceil($records / $rows).'", 
		  "page": "'.$page.'", 
		  "records": "'.$records .'",
		  "rows" : '.json_encode($result).'
		}';
	}
	
	function _insert($data){
		$this->db->insert('t_project',$data);
		return $this->db->insert_id();
	}
	
	function _update($data,$id){
		$this->db->where('project_id',$id);
		$this->db->update('t_project',$data);
		return 1;
	}
	
	function _delete($data){
		$this->db->delete('t_project',$data);
		return 1;
	}
}